package com.chenfan.process.common.utils;

import com.chenfan.ccp.util.tool.HttpUtil;
import com.chenfan.ccp.util.tool.JsonUtil;

import java.util.HashMap;
import java.util.Map;

public class PushUtil {

    private PushUtil() {

    }

    public static PushUtil getInstance() {
        return PushUtilHolder.INSTANCE;
    }

    public void send(PushUtil.PushType pushType, String templateName, String msg) {

        pushType.sendMsg(templateName, msg);
    }

    /**
     * 先简单写出来
     */
    public enum PushType implements PushNotify {
        DINGDING {
            @Override
            public boolean sendMsg(String token, String msg) {
                Map<String, Object> param = new HashMap<>();
                param.put("msgtype", "markdown");
                Map<String, String> markDownParam = new HashMap<>();
                markDownParam.put("title", "待办消息未产生异常提醒");
                markDownParam.put("text", msg);
                param.put("markdown", markDownParam);
                Map<String, Object> atParam = new HashMap<>();
                atParam.put("isAtAll", true);
                param.put("at", atParam);

                String result;

                result = HttpUtil.post(String.format("https://oapi.dingtalk.com/robot/send?access_token=%s", token),
                        JsonUtil.obj2String(param));

                Map map = JsonUtil.string2Obj(result, Map.class);
                return map.getOrDefault("errcode", 0).equals(0);
            }
        }, WEIXIN {
            @Override
            public boolean sendMsg(String templateName, String msg) {
                return false;
            }
        }


    }


    public interface PushNotify {
        boolean sendMsg(String templateName, String msg);
    }

    private static class PushUtilHolder {
        private static final PushUtil INSTANCE = new PushUtil();

    }


}